package com.movie.api.controller;

import com.movie.api.service.FilmService;
import com.movie.api.utils.ExcelReportUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "Echarts图表接口")
@RequestMapping("/api/echarts")
@Slf4j
public class EchartsController {

    @Autowired
    private FilmService filmService;

    /**
     * 饼状图：电影分类
     *
     * @return
     */
    @GetMapping("/bie")
    public List<Map<String, Object>> bie() {
        List<Map<String, Object>> mapList = filmService.getCountByType();
        return mapList;
    }


    /**
     * 柱状图：每种观影类型对应次数
     *
     * @return
     */
    @GetMapping("/bar")
    public Map<String, Object> bar() {
        Map<String, Object> map = filmService.getViewCountByType();
        return map;
    }

    /**
     * 折线图：七天内每天观看次数
     *
     * @return
     */
    @GetMapping("/line")
    public Map<String, Object> line() {
        Map<String, Object> map = filmService.getViewCountByLastSevenDays();
        return map;
    }

    /**
     * 导出电影播放榜单EXCEL报表
     *
     * @param response
     */
    @GetMapping("/export/last")
    public void exportLast(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=近30天电影播放榜单.xlsx");
        Map<String, Object> reportData = filmService.getLast30DaysMovieData();
        if (reportData == null || reportData.isEmpty()) {
            throw new RuntimeException("无可用数据以生成报表");
        }
        InputStream in = getClass().getClassLoader().getResourceAsStream("template/Template4MovieViewRank.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(in);
        ExcelReportUtil.generateExcelReport(workbook, reportData, 0);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        workbook.close();
    }

    /**
     * 导出电影播放榜单EXCEL报表
     *
     * @param response
     */
    @GetMapping("/export/all")
    public void exportAll(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=全部时间电影播放榜单.xlsx");
        Map<String, Object> reportData = filmService.getAllDaysMovieData();
        if (reportData == null || reportData.isEmpty()) {
            throw new RuntimeException("无可用数据以生成报表");
        }
        InputStream in = getClass().getClassLoader().getResourceAsStream("template/Template4MovieViewRank.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(in);
        ExcelReportUtil.generateExcelReport(workbook, reportData, 1);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        workbook.close();
    }
}
